<?php

/**
 * PHP5の関数"file_put_contents"をPHP4でも使えるようにする補助関数
 * 
 * ■ソース元
 * http://www.phpfreaks.com/quickcode/PHP4-file_put_contents/565.php
 *
 * ■参考
 * PHPポケットリファレンス第2版 P.169
 *
 */

if (!function_exists('file_put_contents')) {
    function file_put_contents($filename, $data, $respect_lock = true)
    {
        // Open the file for writing
        $fh = @fopen($filename, 'w');
        if ($fh === false) {
            return false;
        }

        // Check to see if we want to make sure the file is locked before we write to it
        if ($respect_lock === true && !flock($fh, LOCK_EX)) {
            fclose($fh);
            return false;
        }

        // Convert the data to an acceptable string format
        if (is_array($data)) {
            $data = implode('', $data);
        } else {
            $data = (string) $data;
        }

        // Write the data to the file and close it
        $bytes = fwrite($fh, $data);

        // This will implicitly unlock the file if it's locked
        fclose($fh);

        return $bytes;
    }
}
?>